const path = require("path");
const persistentDBPath = path.join(__dirname, "./cookies.bfd");
const Datastore = require("nedb-promises");
const db = Datastore.create(persistentDBPath);
db.persistence.setAutocompactionInterval(1000 * 60 * 30);

async function getCookie(type) {
    const doc = await db.findOne({ 'type': type });
    return doc && doc.cookie;

}

async function setCookie(type, cookie) {
    const doc = await db.findOne({ 'type': type });
    if (doc) {
        await db.update({ 'type': type }, { $set: { 'cookie': cookie } });
    } else {
        await db.insert({ 'type': type, 'cookie': cookie });
    }
}

module.exports = {
    getCookie,
    setCookie
}